var iot = require('iot');
var network = require('network');

var net = network.openNetWorkClient();

var productKey = 'productKey';      /* your productKey */
var deviceName = 'deviceName';      /* your deviceName */
var deviceSecret = 'deviceSecret';  /* your deviceSecret */

var device;
var count = 0;

var topic = '/sys/' + productKey + '/' + deviceName + '/user/haas/info';

function createDevice() {
    device = iot.device({
        productKey: productKey,
        deviceName: deviceName,
        deviceSecret: deviceSecret
    });

    device.on('connect', function () {
        console.log('(re)connected');

        device.subscribe({
            topic: topic,
            qos: 0
        });

        device.unsubscribe(topic);

        device.publish({
            topic: topic,
            payload: 'haas haas haas',
            qos: 1
        });

        var lightswitch = 0;
        var eventid = 'alarmEvent';

        setInterval(function () {
            if (lightswitch) {
                lightswitch = 0;
            } else {
                lightswitch = 1;
            }

            /* post props */
            device.postProps(JSON.stringify({
                LightSwitch: lightswitch
            }));

            /* post event */
            var eventPayload = JSON.stringify({
                alarmType: 0
            });
            device.postEvent({
                id: eventid,
                params: eventPayload
            });

            device.getNtpTime(function(res) {
                console.log('ntp time is ' + res.year + '-' + res.month + '-' + res.day + ' ' + res.hour + ':' + res.minute + ':' + res.second + ' ' + res.msecond)
                console.log('ntp time timestamp ' + res.timestamp)
            });

            console.log(system.memory().heapFree);

        }, 3000);

        /* 云端设置属性事件 */
        device.onProps(function (res) {
            console.log('cloud req msg_id is ' + res.msg_id);
            console.log('cloud req params_len is ' + res.params_len);
            console.log('cloud req params is ' + res.params);
        });

        /* 云端下发服务事件 */
        device.onService(function (res) {
            console.log('received cloud msg_id is ' + res.msg_id);
            console.log('received cloud service_id is ' + res.service_id);
            console.log('received cloud params_len is ' + res.params_len);
            console.log('received cloud params is ' + res.params);
        });
    });

    /* 网络断开事件 */
    device.on('disconnect', function () {
        console.log('disconnect ');
    });

    /* mqtt消息 */
    device.on('message', function (res) {
        console.log('mqtt message')
        console.log('mqtt topic is ' + res.topic);
        console.log('mqtt payload is ' + res.payload);
    })

    /* 关闭连接事件 */
    device.on('end', function () {
        console.log('iot client just closed');
    });

    /* 发生错误事件 */
    device.on('error', function (err) {
        console.log('error ' + err);
    });
}

var status = net.getStatus();
console.log('net status is: ' + status);


if (status == 'connect') {
    createDevice();
} else {
    net.on('connect', function () {
        createDevice();
    });
}